<template>
  <div>
    <el-dialog :title="title" :closeOnClickModal="false" :visible.sync="show" width="650px">
      <el-form label-position="right" :model="selectTable" ref="addForm" label-width="80px" :inline="false" autocomplete="off">
        <el-row>
          <el-col>
            <el-form-item label="产品名称" prop="productName" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.productName" maxLength="20"></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="产品图片" prop="productLogo" :rules="[rule[0]]">
              <singleImage v-model="selectTable.productLogo"></singleImage>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="产品网址" prop="netAddress" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.netAddress" maxLength="200"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style lang="scss" scoped>
.scroller {
  border: 1px #e4e4e4 solid;
  height: 250px;
  overflow: hidden;
  .el-scrollbar {
    height: 267px;
  }
}
</style>

<script>
import * as api from '@/utils/api'
export default {
  components: {
    singleImage: () => import('@/components/Upload/singleImage')
  },
  data() {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONs(this.data)
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        const data = this.JSONs(this.selectTable)
        if (data.method === 'edit') {
          await api.productRecommendModify(data)
        } else if (data.method === 'add') {
          await api.productRecommendSave(data)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>